﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Security;
    using System.Web;

    internal class CreateFolderCommandHandler : XmlCommandHandlerBase
    {
        protected override void BuildXml()
        {
            if (!base.CurrentFolder.CheckAcl(AccessControlRules.FolderCreate))
            {
                ConnectorException.Throw(0x67);
            }
            string fileName = HttpContext.Current.Request.QueryString["newFolderName"];
            if (!CKFinder.Connector.Connector.CheckFileName(fileName) || Config.Current.CheckIsHiddenFolder(fileName))
            {
                ConnectorException.Throw(0x66);
            }
            else
            {
                string path = Path.Combine(base.CurrentFolder.ServerPath, fileName);
                bool flag = false;
                if (Directory.Exists(path))
                {
                    ConnectorException.Throw(0x73);
                }
                try
                {
                    Util.CreateDirectory(path);
                    flag = true;
                }
                catch (ArgumentException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (PathTooLongException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (SecurityException)
                {
                    ConnectorException.Throw(0x68);
                }
                catch (ConnectorException exception)
                {
                    throw exception;
                }
                catch (Exception)
                {
                    ConnectorException.Throw(110);
                }
                if (flag)
                {
                    XmlUtil.SetAttribute(XmlUtil.AppendElement(base.ConnectorNode, "NewFolder"), "name", fileName);
                }
            }
        }
    }
}

